<?php
/* 
 * ebiz front
 * 
 * ProductController
 * 
 * @author allen
 * 
 */
require_once '../app/core/controllers/BaseController.php';
require_once '../app/views/BaseView.php';
require_once '../app/views/ProductView.php';
require_once '../app/services/ProductService.php';
require_once '../app/services/OrderService.php';
require_once '../app/services/QueryService.php';
require_once '../app/services/CategoryService.php';
require_once '../app/services/CustomerService.php';

require_once '../app/constants/ConfigConstants.php';

class ProductController extends BaseController {
    
//	const CategoryId = 21;
	
    public function index($param = array()){
        CommonUtil::startTimeFunction();
        $config = $GLOBALS["config"];
        require_once "../". $GLOBALS["local"] ."/"."myaccount.php";
        //get the header and footer
        $header = new HeaderView();
        $footer = new FooterView();
        $headerView = $header->getCommonHeaderView();
        $footerView = $footer->getCommonFooterView();
        //get the item per page from config table
        $itemsPerPage = $config['items_per_page'];
        $result = array();
        $customerService = new CustomerService();
        $orderService = new OrderService();
        $itemId = $param['itemId']; //set in DirectUtil.php
    	$productUrlTitle = urldecode($param['productUrlTitle']); 
//    	$productUrlTitle = $param['productUrlTitle']; 
    	$productService = new ProductService();
        $productInfo = array();
    	$productInfo = $productService->getProductInProgramByUrlTitle($productUrlTitle);
    	if(empty($itemId)){
            $productItemInfo = $productService->getProductByUrlTitle($productUrlTitle);
    	}else{
            $productItemInfo = $productService->getProductByUrlTitleAndItemId($productUrlTitle, $itemId);
    	}
    	$defaultItemId = $productItemInfo['show_itemid'][0];
    	//if $defaultItemId is empty then header to empty product page
    	if( empty($defaultItemId) ){
            HttpUtil::renderEmptyProduct();
    	}
    	//show the metadata info in product Page , include primary metadata and all metadata
        $metadataInfo = $productService->getMetadataInfoByItemId($defaultItemId);
        //get the product id to get the query info
        $pid = $productItemInfo["item_".$defaultItemId]["product_id"];
        //use in opinion , base on pid to find item ids//split by ','
        $itemIdStrings = $productService->getItemIdsByProductId($pid);
        //get the navigation info
        $arr_category = $productService->getByProductId($pid);
        //get the all query info
        $queryService = new QueryService();
        //$itemsPerPage akimimi set is 5
        $queryAll = $queryService->getQueryByProductId($pid, 0, 5);
        $queryAllNum = $queryService->getQueryByProductId($pid, 0, 5, "", true);
        $queryGoods = $queryService->getQueryByProductId($pid, 0, 5, 1);
        $queryGoodsNum = $queryService->getQueryByProductId($pid, 0, 5, 1, true);
        $queryDistribute = $queryService->getQueryByProductId($pid, 0, 5, 2);
        $queryDistributeNum = $queryService->getQueryByProductId($pid, 0, 5, 2, true);
        $queryPayment = $queryService->getQueryByProductId($pid, 0, 5, 3);
        $queryPaymentNum = $queryService->getQueryByProductId($pid, 0, 5, 3, true);
        $queryCampuses = $queryService->getQueryByProductId($pid, 0, 5, 4);
        $queryCampusesNum = $queryService->getQueryByProductId($pid, 0, 5, 4, true);
        $queryMaintain = $queryService->getQueryByProductId($pid, 0, 5, 5);
        $queryMaintainNum = $queryService->getQueryByProductId($pid, 0, 5, 5, true);
        //get the campuses name and sale num if user already login
        $result[]["campuses_name"] = "";
        $result[]["sale_num"] = "";
        if(AAService::isLogin()){//if user already login
            $customerId = AAService::getCustomer();
            //show the campuses of current user
            $campusesName = $customerService->getCustomerCampusesNameByCid($customerId);
            $result[]["campuses_name"] = $campusesName[0]["area_name"];
            //show the campuses already sale number
	        $saleNum = $orderService->getSuccessOrderItemNumByItemId($defaultItemId);
	        $result[]["sale_num"] = $saleNum;
        }
        //show the metadata info include primary metadata and all metadata
        $result["metadata_info"] = $metadataInfo;
        //show the all query type
        $result["query_all"] = $queryAll;
        $result["query_all_num"] = $queryAllNum[0]["ROWS"];
        $result["query_goods"] = $queryGoods;
        $result["query_doogs_num"] = $queryGoodsNum[0]["ROWS"];
        $result["query_distribute"] = $queryDistribute;
        $result["query_distribute_num"] = $queryDistributeNum[0]["ROWS"];
        $result["query_payment"] = $queryPayment;
        $result["query_payment_num"] = $queryPaymentNum[0]["ROWS"];
        $result["query_campuses"] = $queryCampuses;
        $result["query_campuses_num"] = $queryCampusesNum[0]["ROWS"];
        $result["query_maintain"] = $queryMaintain;
        $result["query_maintain_num"] = $queryMaintainNum[0]["ROWS"];
        
        $result["product_id"] = $pid;
        $result["product_item_ids"] = $itemIdStrings;
        $result["arr_category"] = $arr_category;
        $result["product_title"] = $productInfo[1]['product']['title'];
        $result["isInStock"] = $productItemInfo["item_".$defaultItemId]["inventory"];
        //top level category url title
        $result["category_url_title"] = $arr_category[0]["categoryUrlTitle"];// ==mobile_and_accessories 不显示下拉列表框
        $result["header_view"] = $headerView;
        $result["footer_view"] = $footerView;
    	$productView = new ProductView();
    	echo $productView->render($productUrlTitle, $productInfo[1], $productItemInfo, $result);
    	CommonUtil::endTimeFunction();
    	CommonUtil::getDBConnectionNumber();
    	exit();
    }
    
//    /**
//     * $arrCategory 为当前产品上级所有category的url title集合
//     */
//    private function judgeIsMobile($arrCategory){//判断是否为手机，手机则不生成第二个下拉列表框
//        $isMobile = false;
//        $categoryId = ProductController::CategoryId;//当前手机的category id
//        foreach ($arrCategory as $item) {//$arrCategory 是当前产品的所有category上级
//            if(in_array($categoryId, $item)){
//                $isMobile = true;
//            }
//        }
//        return $isMobile;
//    }
    
}





